//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     追号详情业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Enum.Common;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Json.Entity.WebAPI;
using CL.Redis.BLL;
using CL.Tools.Common;
using CL.View.Entity.Game;
using System;
using System.Collections.Generic;
using System.Linq;
using CL.Enum.Common.Lottery;
using CL.Enum.Common.Status;

namespace CL.Game.BLL
{

    /// <summary>
    /// ChaseTaskDetailsBLL 业务
    /// </summary>
    public class ChaseTaskDetailsBLL
    {
        ChaseTaskDetailsDAL dal = new ChaseTaskDetailsDAL(DbConnectionEnum.CaileGame);

        Log log = new Log("ChaseTaskDetailsBLL");
        /// <summary>
        /// 查询追号详情单个对象
        /// </summary>
        /// <param name="ChaseDetailsID"></param>
        /// <returns></returns>
        public ChaseTaskDetailsEntity QueryEntity(long ChaseDetailsID)
        {
            return dal.QueryEntity(ChaseDetailsID);
        }
        /// <summary>
        /// 更新追号详情对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long ModifyEntity(ChaseTaskDetailsEntity entity)
        {
            return dal.ModifyEntity(entity);
        }
        /// <summary>
        /// 更新发送状态
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <returns></returns>
        public bool ModifySendOut(long ChaseTaskDetailsID)
        {
            return dal.ModifySendOut(ChaseTaskDetailsID);
        }
        /// <summary>
        /// 是否发送
        /// </summary>
        /// <param name="ChaseTaskDetailsID"></param>
        /// <returns></returns>
        public bool IsSendOut(long ChaseTaskDetailsID)
        {
            return dal.IsSendOut(ChaseTaskDetailsID);
        }
        /// <summary>
        /// 停止追号查询
        /// </summary>
        /// <param name="ChaseTaskID"></param>
        /// <param name="QueryType">查询类型  1 追号完成停止、2 追号中奖或中奖奖金大于多少停止</param>
        /// <returns></returns>
        public long StopChaseTaskQuery(long ChaseTaskID, int QueryType)
        {
            return dal.StopChaseTaskQuery(ChaseTaskID, QueryType);
        }
        /// <summary>
        /// 停止追号
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <param name="ChaseTaskDetailsID"></param>
        /// <param name="Amount"></param>
        /// <param name="StopType">1追号完成停止 ,2追号满足条件停止</param>
        /// <returns></returns>
        public long StopChaseTask(long SchemeID, long ChaseTaskDetailsID, long Amount, int StopType)
        {
            return dal.StopChaseTask(SchemeID, ChaseTaskDetailsID, Amount, StopType);
        }
        /// <summary>
        /// 查询追号需要停止的任务
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <param name="ChaseTaskID"></param>
        /// <returns></returns>
        public List<ChaseTaskDetailsEntity> QueryModelListByChaseTaskID(long SchemeID, long ChaseTaskID)
        {
            return dal.QueryModelListByChaseTaskID(SchemeID, ChaseTaskID);
        }
        /// <summary>
        /// 根据方案编号查询对象集
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <returns></returns>
        public List<ChaseTaskDetailsEntity> QueryEntitysBySchemeID(long SchemeID)
        {
            return dal.QueryEntitysBySchemeID(SchemeID);
        }
        /// <summary>
        /// 查询追号内容
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <returns></returns>
        public List<udv_ChaseDetailRecord> QueryChaseDetailRecord(long SchemeID)
        {
            return dal.QueryChaseDetailRecord(SchemeID);
        }
        /// <summary>
        /// 查询期号过期处理追号失败数据
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <param name="IsuseName"></param>
        /// <returns></returns>
        public List<udv_ExpireRevokeChase> QueryExpireRevokeChase(int LotteryCode, string IsuseName)
        {
            return dal.QueryExpireRevokeChase(LotteryCode, IsuseName);
        }

        #region 自定义方法
        /// <summary>
        /// 追号记录
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="OrderCode"></param>
        /// <returns></returns>
        public ChaseRecordResult ChaseRecord(long UserCode, long OrderCode)
        {
            ChaseRecordResult result = null;
            try
            {
                var Entity = new BusinessRedis().ChaseRecordRedis(UserCode, OrderCode);
                if (Entity == null)
                {
                    var SchemeChaseEntitys = new ChaseTaskDetailsBLL().QueryEntitysBySchemeID(OrderCode);
                    var SchemeEticketEntitys = new SchemeETicketsBLL().QueryEntitysBySchemeID(OrderCode);
                    var SchemeEntity = new SchemesBLL().QueryEntity(OrderCode);
                    var ChaseTaskEntity = new ChaseTasksBLL().QueryEntityBySchemeID(OrderCode);
                    var Data_Already = SchemeChaseEntitys.Where(w => w.IsExecuted == true).Select(s => new AlreadyEntity()
                    {
                        Amount = s.Amount,
                        ChaseDetailCode = s.ID,
                        ChaseStatus = SchemeEticketEntitys.Where(w => w.ChaseTaskDetailsID == s.ID).Sum(so => so.WinMoney) > 0 ? "中奖" : SchemeEticketEntitys.Where(w => w.ChaseTaskDetailsID == s.ID && w.TicketStatus == (byte)TicketStatusEnum.NoWinning).ToList().Count > 0 ? "未中奖" : s.QuashStatus == (short)QuashStatusEnum.NoQuash ? "待开奖" : "撤单",
                        IsuseNum = new IsusesBLL().QueryIssueName(s.IsuseID), //待维护
                        WinMoney = SchemeEticketEntitys.Where(w => w.ChaseTaskDetailsID == s.ID).Sum(so => so.WinMoney)
                    }).OrderBy(o => o.ChaseDetailCode).ToList();
                    var Data_Surplus = SchemeChaseEntitys.Where(w => w.IsExecuted == false).Select(s => new SurplusEntity()
                    {
                        Amount = s.Amount,
                        ChaseDetailCode = s.ID,
                        ChaseStatus = s.QuashStatus == (short)QuashStatusEnum.NoQuash ? "等待追号" : "撤单",
                        IsuseNum = new IsusesBLL().QueryIssueName(s.IsuseID)
                    }).OrderBy(o => o.ChaseDetailCode).ToList();
                    Entity = new ChaseRecordEntity()
                    {
                        Already = Data_Already.Count(),
                        AlreadyData = Data_Already,
                        Amount = SchemeEntity.SchemeMoney,
                        IsuseCount = ChaseTaskEntity.IsuseCount,
                        LotteryCode = SchemeEntity.LotteryCode,
                        LotteryName = Common.GetDescription((LotteryInfo)SchemeEntity.LotteryCode),
                        Number = SchemeEntity.LotteryNumber,
                        OrderStatus = SchemeEntity.SchemeStatus,
                        Stops = ChaseTaskEntity.StopTypeWhenWinMoney,
                        WinMoney = SchemeEticketEntitys.Sum(s => s.WinMoney),
                        Surplus = Data_Surplus.Count(),
                        SurplusData = Data_Surplus
                    };
                }
                result = new ChaseRecordResult()
                {
                    Code = (int)ResultCode.Success,
                    Msg = Common.GetDescription(ResultCode.Success),
                    Data = Entity
                };
            }
            catch (Exception ex)
            {
                log.Write("追号记录错误[ChaseRecord]：" + ex.StackTrace, true);
                result = new ChaseRecordResult()
                {
                    Code = (int)ResultCode.Error,
                    Msg = Common.GetDescription(ResultCode.Error)
                };
            }
            return result;
        }

        /// <summary>
        /// 追号详情
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="ChaseDetailCode"></param>
        /// <returns></returns>
        public ChaseDetailResult ChaseDetail(long UserCode, long ChaseDetailCode)
        {
            ChaseDetailResult result = null;
            try
            {
                var Entity = new BusinessRedis().ChaseDetailRedis(UserCode, ChaseDetailCode);
                if (Entity == null)
                {
                    var ChaseEntity = this.QueryEntity(ChaseDetailCode);
                    var SchemeEntity = new SchemesBLL().QueryEntity(ChaseEntity.SchemeID);
                    var SchemeWinEntity = new SchemesWinBLL().QueryEntitysBySchemeID(ChaseEntity.SchemeID);
                    var IsuseEntity = new IsusesBLL().QueryEntity(ChaseEntity.IsuseID);
                    var SchemeEticketEntitys = new SchemeETicketsBLL().QueryEntitysBySchemeID(ChaseEntity.SchemeID);
                    long WinMoney = SchemeWinEntity == null ? 0 : SchemeWinEntity.Sum(s => s.WinMoneyNoWithTax);
                    int TicketStatus_Count = SchemeEticketEntitys.Where(w => w.ChaseTaskDetailsID == ChaseDetailCode && w.TicketStatus == (byte)TicketStatusEnum.NoWinning).Count();
                    int TicketStatus2_Count = SchemeEticketEntitys.Where(w => w.ChaseTaskDetailsID == ChaseDetailCode && w.TicketStatus == (byte)TicketStatusEnum.InTickeSuccess).Count();
                    Entity = new ChaseDetailEntity()
                    {
                        Amount = ChaseEntity.Amount,
                        IsOpened = IsuseEntity.IsOpened,
                        IsuseNum = IsuseEntity.IsuseName,
                        LotteryCode = SchemeEntity.LotteryCode,
                        Number = SchemeEntity.LotteryNumber,
                        OpenNumber = IsuseEntity.OpenNumber,
                        OrderNum = SchemeEntity.SchemeNumber,
                        OrderStatus = ChaseEntity.QuashStatus != (short)QuashStatusEnum.NoQuash ? 3 : TicketStatus2_Count > 0 ? 4 : WinMoney > 0 ? 1 : TicketStatus_Count > 0 ? 0 : 2,
                        OrderTime = SchemeEntity.CreateTime.ToString("yyyyMMddHHmmss"),
                        WinMoney = WinMoney
                    };
                }
                if (string.IsNullOrEmpty(Entity.OpenNumber.Trim()))
                {
                    var ChaseEntity = this.QueryEntity(ChaseDetailCode);
                    var IsuseEntity = new IsusesBLL().QueryEntity(ChaseEntity.IsuseID);

                    Entity.OpenNumber = IsuseEntity.OpenNumber;
                }
                result = new ChaseDetailResult()
                {
                    Code = (int)ResultCode.Success,
                    Msg = Common.GetDescription(ResultCode.Success),
                    Data = Entity
                };
            }
            catch (Exception ex)
            {
                log.Write("追号详情错误[ChaseRecord]：" + ex.StackTrace, true);
                result = new ChaseDetailResult()
                {
                    Code = (int)ResultCode.Error,
                    Msg = Common.GetDescription(ResultCode.Error)
                };
            }

            return result;
        }
        /// <summary>
        /// 未完成追号撤单
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <param name="ChaseTaskDetailID"></param>
        /// <param name="UserID"></param>
        /// <param name="Amount"></param>
        /// <returns></returns>
        public int ChaseRevoke(long SchemeID, long ChaseTaskDetailID, long UserID, long Amount)
        {
            return dal.ChaseRevoke(SchemeID, ChaseTaskDetailID, UserID, Amount);
        }

        #endregion
    }
}
